<template>
  <!-- <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"> -->
    <div id="mescustomerm-add-or-update">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="客户名称" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="是否启用">
      <!-- <el-input v-model="dataForm.enabledFlg" placeholder="是否启用"></el-input> -->
      <el-select v-model="dataForm.enabledFlg" placeholder="是否启用">
        <el-option
          v-for="item in optionsFlg"
          :key="item.code"
          :label="item.name"
          :value="item.code">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="联系人">
      <el-input v-model="dataForm.contactUser" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="联系方式">
      <el-input v-model="dataForm.contactPhone" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="企业地址">
      <el-input v-model="dataForm.customerAdress" placeholder="企业地址"></el-input>
    </el-form-item>
    <el-form-item label="企业规模">
      <el-input v-model="dataForm.companyScale" placeholder="企业规模"></el-input>
    </el-form-item>
    <el-form-item label="合作关系">
      <!-- <el-input v-model="dataForm.relationShip" placeholder="合作关系"></el-input> -->
      <el-select v-model="dataForm.relationShip" placeholder="合作关系">
        <el-option
          v-for="item in relationType"
          :key="item.code"
          :label="item.name"
          :value="item.code">
        </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <div class="footer">
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
      <el-button @click="cancle">取消</el-button>
    </div>
    </div>
  <!-- </el-dialog> -->
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        customerNum: '',
        customerName: '',
        enabledFlg: '',
        contactUser: '',
        contactPhone: '',
        customerAdress: '',
        companyScale: '',
        relationShip: ''
      },
      dataRule: {
        customerName: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' }
        ]
      },
      optionsFlg: [],
      relationType: []
    }
  },
  mounted() {
    this.optionLists()
    this.init(this.$route.params.id)
  },
  methods: {
    init(id) {
      this.dataForm.customerNum = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.customerNum) {
          this.$http({
            url: this.$http.adornUrl(
              `/basic/mescustomerm/info/${this.dataForm.customerNum}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.mesCustomerM
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/basic/mescustomerm/${
                !this.dataForm.customerNum ? 'save' : 'update'
              }`
            ),
            method: 'post',
            data: this.$http.adornData({
              customerNum: this.dataForm.customerNum || undefined,
              customerName: this.dataForm.customerName,
              enabledFlg: this.dataForm.enabledFlg,
              contactUser: this.dataForm.contactUser,
              contactPhone: this.dataForm.contactPhone,
              customerAdress: this.dataForm.customerAdress,
              companyScale: this.dataForm.companyScale,
              relationShip: this.dataForm.relationShip
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  // this.visible = false
                  // this.$emit('refreshDataList')
                  eventBus.$emit('closeCurrentPage')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // 取消
    cancle() {
      eventBus.$emit('closeCurrentPage')
    },
    optionLists() {
      let arr = ['sys_yes_no', 'relation_type']
      this.$http({
        url: this.$http.adornUrl('/basic/mescodemasterm/dataList'),
        method: 'post',
        data: this.$http.adornParams(arr, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          data.sys_yes_no.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.optionsFlg.push(obj)
          })
          data.relation_type.forEach((item, index) => {
            let obj = {}
            obj.code = item.code
            obj.name = item.name
            this.relationType.push(obj)
          })
        } else {
        }
        this.dataListLoading = false
      })
    }
  }
}
</script>
<style scoped lang="scss">
#mescustomerm-add-or-update {
  width: 60%;
  margin: 0 auto;
  .footer {
    text-align: center;
  }
  .el-form-item {
    .el-select {
      width: 500px;
    }
    .el-input {
      width: 500px;
    }
  }
}
</style>
